//
//  ZYNetworkTools.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/11.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit
import AFNetworking

enum ZYNetworkToolsMethod:String {
    case GET = "GET"
    case POST = "POST"
}


class ZYNetworkTools: AFHTTPSessionManager {
    
    // MARK: - 单例
    //全局访问
    static let sharedTools:ZYNetworkTools = {
        let tools = ZYNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    func request(method:ZYNetworkToolsMethod, URLString: String, parameters: AnyObject?, success: (response:AnyObject?) -> Void, failure: (error: NSError) -> Void){
    
        if method == .GET {
            GET(URLString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                success(response:response)
            }, failure: { (_, error) -> Void in
                failure(error:error)
            })
            
        }else{
            POST(URLString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                    //03    执行
                    success(response: response)
                }, failure: { (_, error) -> Void in
                    failure(error: error)
            })
        }
    
    }
    

    
}

//MARK: - 首页Home接口
extension ZYNetworkTools{
    func homeLoadData(sinceId:Int64, maxId:Int64, success: (response: AnyObject?) -> Void, failure: (error: NSError) -> Void){
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params = [
            "access_token":ZYUserAccountViewModel.sharedTools.access_token!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        ]
        
        request(ZYNetworkToolsMethod.GET, URLString: urlString, parameters: params, success: success, failure: failure)
    }
    
}

//MARK: - 授权界面接口
extension ZYNetworkTools{
    //请求授权
    func oauthLoadUserAccount(code:String, success: (response: AnyObject?) -> Void, failure: (error: NSError) -> Void){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = [
            "client_id":WBAppkey,
            "client_secret": WBAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri": WBRedirect_uri
        ]

        request(ZYNetworkToolsMethod.POST, URLString: urlString, parameters: params, success: success, failure: failure)
        
    }
    
    //请求个人信息
    func oauthLoadUserInfo(userAccount:ZYUserAccountModel, success: (response: AnyObject?) -> Void, failure: (error: NSError) -> Void){
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]

        request(ZYNetworkToolsMethod.GET, URLString: urlString, parameters: params, success: success, failure: failure)
        
    }
    

    
}
//MARK: - 发布wb 请求
extension ZYNetworkTools {
    
    func composeUpdate(status: String, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求的url
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        // 请求参数
        let params = [
            "access_token": ZYUserAccountViewModel.sharedTools.access_token!,
            "status": status
        ]
        // 发送请求
        request(ZYNetworkToolsMethod.POST, URLString: urlString, parameters: params, success: success, failure: failure)
    }

    // 发布图片微博
    func composeUpload(status: String, images:[UIImage] , success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求的urlString
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        // 请求参数
        let params = [
            "access_token": ZYUserAccountViewModel.sharedTools.access_token!,
            "status": status
        ]

        // 发送请求
        POST(urlString, parameters: params, constructingBodyWithBlock: { (formdata) -> Void in
            //目前的接口 ，仅仅支持上传一张图片
            let data = UIImagePNGRepresentation(images.first!)!
            formdata.appendPartWithFileData(data, name: "pic", fileName: "fileName", mimeType: "application/octet-stream")
            }, progress: nil, success: { (_, response) -> Void in
                success(response: response)
            }) { (_, error) -> Void in
                failure(error: error)
        }
        
    }

    
    
    
    
}





